<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
          >批量禁用</el-button
        >
        <el-button
          type="success"
          icon="el-icon-plus"
          class="handle-del mr10"
          @click="addOne"
          >增加</el-button
        >
        <el-input
          v-model="query.roleName"
          placeholder="角色名"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
          :selectable="checkSelectable"
        ></el-table-column>
        <el-table-column prop="roleName" label="角色名" align="center"></el-table-column>
        <el-table-column
          prop="roleSign"
          label="角色标志"
          align="center"
        ></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              color="Whitesmoke"
              :type="
                scope.row.isDel === 1 ? 'danger' : scope.row.isDel === 0 ? 'success' : ''
              "
              >{{ scope.row.isDel === 0 ? "正常" : "禁用" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createUser"
          label="创建者"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="remarks"
          label="备注"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="130" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >禁用</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--编辑-->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%" @close="dialogClose">
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="角色名" prop="roleName">
          <el-input type="text" v-model="form.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色标志" prop="roleSign">
          <el-input type="text" v-model="form.roleSign"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="isDel">
          <el-switch
            v-model="form.isDel"
            active-text="禁用"
            :active-value="1"
            inactive-text="正常"
            :inactive-value="0"
            active-color="#ff4949"
            inactive-color="#13ce66"
          ></el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="text" v-model="form.remarks"></el-input>
        </el-form-item>
        <el-form-item label="菜单" prop="menuIds">
          <el-tree
            ref="menutree"
            :data="menus"
            show-checkbox
            accordion
            check-strictly
            node-key="id"
            :default-checked-keys="form.menus"
            :props="treeProps"
          >
          </el-tree>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
var _this;
export default {
  name: "basetable",
  data() {
    return {
      query: {
        roleName: "",
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      form: {},
      rowdata: {},
      rules: {
        roleName: { required: true, message: "角色名称不能为空", trigger: "blur" },
        roleSign: { required: true, message: "角色标志不能为空", trigger: "blur" },
      },
      menus: [],
      treeProps: {
        children: "children",
        label: "title",
      },
    };
  },
  created() {
    this.getData();
    _this = this;
  },
  methods: {
    // 获取数据
    getData() {
      this.$post("/role/getall", this.query).then((res) => {
        if (res.code === 0) {
          this.tableData = res.data;
        } else {
          this.$message.error("查询失败！");
        }
      });
    },
    getMenuRole(roleId) {
      this.$get("/menu/menurole/" + roleId, null).then((res) => {
        if (res.code === 0) {
          this.menus = res.data.menutree;
          this.form.menus = res.data.checked;
        } else {
          this.$message.error("查询菜单失败！");
        }
      });
    },
    checkSelectable(row) {
      return row.isDel === 0;
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要禁用吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delList.push(row.id);
          this.deleteList(this.delList);
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      this.delList = this.multipleSelection.map((x) => {
        return x.id;
      });
      this.deleteList(this.delList);
      this.multipleSelection = [];
    },
    //删除操作
    deleteList(param) {
      this.$post("/role/logicdel", param).then((res) => {
        if (res.code === 0) {
          this.$message.success("禁用成功");
          this.getData();
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //编辑操作
    handleEdit(index, row) {
      this.getMenuRole(row.id);
      this.rowdata = row;
      this.form = JSON.parse(JSON.stringify(row));
      this.editVisible = true;
    },
    // 增加
    addOne(){
      this.getMenuRole(0);
      this.editVisible = true;
    },
    //保存编辑
    saveEdit() {
      this.form.menus = JSON.parse(JSON.stringify(this.$refs.menutree.getCheckedKeys()));
      (this.$refs.menutree.getHalfCheckedKeys()).forEach(element => {
        this.form.menus.push(element)
      });
      
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$post("/role/saveorupdate", this.form).then((res) => {
            if (res.code === 0) {
              this.$message.success(res.msg);
              this.form = {};
              this.rowdata = {};
              this.getData();
              this.editVisible = false;
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
    // 弹窗关闭
    dialogClose(){
      this.form = {};
      this.menus = [];
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 150px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
/deep/.el-upload {
  width: 150px;
  height: 210px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 210px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 210px;
  display: block;
}
</style>
